package cn.xshi.sys.init;

import cn.xshi.sys.init.task.InitTask;
import cn.xshi.sys.model.*;
import cn.xshi.sys.service.*;
import cn.xshi.common.cache.redis.RedisUtil;
import cn.xshi.common.constant.CacheConstant;
import cn.xshi.common.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ForkJoinPool;
/**
 * 全局缓存
 */
@Component
@Slf4j
public class GlobalPersistentComponent {

    @Resource
    RedisUtil redisUtil;

    @Resource
    SysDataDictionaryService sysDataDictionaryService;

    @Resource
    SysPathService sysPathService;

    @Resource
    SysIpFrozenService sysIpFrozenService;

    @Resource
    SysConstantService sysConstantService;

    @Resource
    SysAreaRegionService sysAreaRegionService;

    /**
     * 加载数据字典，平台常量及平台路径到缓存中
     */
    public void initSysDataDictionary(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long millis1 = System.currentTimeMillis();
        Map<String, Object> condition = new HashMap<String, Object>();
        List<SysDataDictionary> sysDataDictionaryList = sysDataDictionaryService.getSysDataDictionaryListAllByCondition(condition);
        long millis2 =  System.currentTimeMillis();
        log.info(sdf.format(new Date())+"--->读取数据字典耗时:"+(millis2-millis1)+"毫秒");
        log.info(sdf.format(new Date())+"--->加载缓存配置开始");
        millis2 =  System.currentTimeMillis();
        redisUtil.hset(CacheConstant.SYSHASH,CacheConstant.SysDataDictionaryCACHE, JsonUtil.toFastJson(sysDataDictionaryList));
        log.info(sdf.format(new Date())+"--->将数据存入缓存耗时:"+(millis2-millis1)+"毫秒");
        log.info(sdf.format(new Date())+"--->加载缓存配置结束");
    }

    /**
     * 初始化区域至缓存中
     */
    public void initSysAreaRegion(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long millis1 = System.currentTimeMillis();
        Map<String, Object> condition = new HashMap<String,Object>();
        List<SysAreaRegion> list = sysAreaRegionService.getSysAreaRegionListByCondition(condition);
        long millis2 =  System.currentTimeMillis();
        log.info(sdf.format(new Date())+"--->读取行政区域实例耗时:"+(millis2-millis1)+"毫秒");
        log.info(sdf.format(new Date())+"--->加载行政区域实例缓存开始");
        redisUtil.hset(CacheConstant.SYSHASH,CacheConstant.SysAreaRegionCACHE,JsonUtil.toFastJson(list));
        log.info(sdf.format(new Date())+"--->加载行政区域实例缓存结束");
    }

    /**
     *
     */
    public void initForkJoinPool(){
        long millis1 = System.currentTimeMillis();
        int size = Runtime.getRuntime().availableProcessors();//获取本系统的有效线程数，设置线程池为有效线程的两倍。
        ForkJoinPool forkJoinPool = new ForkJoinPool(size*2);
        try {
            Map<String, Object> condition = new HashMap<String,Object>();
            condition = new HashMap<String, Object>();
            condition.put("status", 2);
            List<SysIpFrozen> sysIpFrozenList = sysIpFrozenService.getSysIpFrozenListAllByCondition(condition);
            InitTask initTask = new InitTask(sysIpFrozenList,10);
            //方法一 同步
            Integer result = forkJoinPool.invoke(initTask);
//                //线程阻塞，等待所有任务完成
//                forkJoinPool.awaitTermination(forkJoinTimeOut, TimeUnit.SECONDS);


//                //方法二 异步
//                ForkJoinTask forkJoinTask = forkJoinPool.submit(initTask);
//                result = new Integer(forkJoinTask.get());


//                //方法三 异步
//                Future<Integer> futureResult = forkJoinPool.submit(initTask);
//                result = futureResult.get();
            long millis2 =  System.currentTimeMillis();
            log.info("存取黑名单实例耗时:"+(millis2-millis1)+"毫秒");
        }catch (Exception e){
            if(null != forkJoinPool){
                forkJoinPool.shutdown();
            }
        }finally {
            if(null != forkJoinPool){
                forkJoinPool.shutdown();
            }
        }
    }

    /**
     * 加载路径
     */
    public void initPath(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long millis1 = System.currentTimeMillis();
        Map<String, Object> condition = new HashMap<String, Object>();
        log.info(sdf.format(new Date())+"--->读取平台路径开始");
        List<SysPath> sysPathList = sysPathService.getSysPathListAllByCondition(condition);
        redisUtil.del(CacheConstant.SysPathCACHE);
        redisUtil.hset(CacheConstant.SYSHASH,CacheConstant.SysPathCACHE,JsonUtil.toFastJson(sysPathList));
        long millis2 =  System.currentTimeMillis();
        millis2 =  System.currentTimeMillis();
        log.info(sdf.format(new Date())+"--->读取平台路径缓存耗时:"+(millis2-millis1)+"毫秒");
    }

    /**
     * 加载黑户ip
     */
    public void initSysIpFrozen(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long millis1 = System.currentTimeMillis();
        Map<String, Object> condition = new HashMap<String, Object>();
        log.info(sdf.format(new Date())+"--->读取IP黑户开始");
        condition = new HashMap<String, Object>();
        condition.put("status", 2);
        List<SysIpFrozen> sysIpFrozenList = sysIpFrozenService.getSysIpFrozenListAllByCondition(condition);
        redisUtil.hset(CacheConstant.SYSHASH,CacheConstant.SysIpFrozenCACHE,JsonUtil.toFastJson(sysIpFrozenList));
        log.info(sdf.format(new Date())+"--->一共加载了:"+ sysIpFrozenList.size()+"条平IP黑户数据");
        long millis2 =  System.currentTimeMillis();
        log.info(sdf.format(new Date())+"--->读取IP黑户缓存耗时:"+(millis2-millis1)+"毫秒");
        initForkJoinPool();
    }

    /**
     * 加载常量
     */
    public void initSysConstant(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long millis1 = System.currentTimeMillis();
        Map<String, Object> condition = new HashMap<String, Object>();
        log.info(sdf.format(new Date())+"--->读取平台常量开始");
        condition = new HashMap<String, Object>();
        List<SysConstant> sysConstantList = sysConstantService.getSysConstantListAllByCondition(condition);
        redisUtil.hset(CacheConstant.SYSHASH,CacheConstant.SysConstantCACHE,JsonUtil.toFastJson(sysConstantList));
        long millis2 =  System.currentTimeMillis();
        millis2 =  System.currentTimeMillis();
        log.info(sdf.format(new Date())+"--->一共加载了:"+ sysConstantList.size()+"条平台常量数据");
        log.info(sdf.format(new Date())+"--->读取平台常量缓存耗时:"+(millis2-millis1)+"毫秒");
    }
}
